package org.company.tool.service;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.company.tool.bean.ColumnBean;
import org.company.tool.util.DBManager;
import org.company.tool.util.StringUtil;
/**
 * @author hucw
 *
 */
public abstract class EntityTransfer {
	/**
	 * 解析数据库字段，转化为ColumnBean类型的数据
	 * @param entityColumn
	 * @return 
	 * @throws SQLException
	 */
	public List<ColumnBean> parseColumn(String tableName) throws SQLException {
		Connection conn = DBManager.getInstance().getConnection();
		DatabaseMetaData meta = conn.getMetaData();
		ResultSet rs = this.getColumns(meta, tableName);
		List<String> primaryKeys = this.getPrimaryKeys(meta,tableName);
		List<ColumnBean> list = new ArrayList<ColumnBean>();
		ColumnBean bean = null;
		while(rs.next()) {
			bean = new ColumnBean();
			bean.setColumn(rs.getString("COLUMN_NAME").toUpperCase());
			bean.setProperty(StringUtil.nameTransfer(rs.getString("COLUMN_NAME").toLowerCase()));
			bean.setDataType(this.sqlTypeToJavaType(rs.getInt("DATA_TYPE"),rs.getInt("COLUMN_SIZE"),rs.getInt("DECIMAL_DIGITS")));
			bean.setJdbcType(this.sqlTypeToJavaType(rs.getInt("DATA_TYPE"),rs.getInt("COLUMN_SIZE"),rs.getInt("DECIMAL_DIGITS")));
			bean.setUpperProperty(bean.getProperty().replaceFirst(bean.getProperty().substring(0,1), bean.getProperty().substring(0,1).toUpperCase()));
			bean.setIsPrimaryKey(primaryKeys.contains(bean.getColumn()));
			bean.setColumnSize(rs.getInt("COLUMN_SIZE"));
			list.add(bean);
		}
		rs.close();
		conn.close();
		return list;
	}
	
	/**
     * 数据库数据类型与java数据类型的映射
     * @param type
     * @param columnSize
     * @param decimalDigits
     * @return
     */
    protected String sqlTypeToJavaType(int type,int columnSize,int decimalDigits) {
        String javaType = null;
        switch(type) {
            case Types.SMALLINT:
            case Types.INTEGER:
                javaType = "Integer";
                break;
            case Types.BIGINT:
                javaType = "Long";
            case Types.FLOAT:
            case Types.DOUBLE:
                javaType = "Double";
                break;
            case Types.NUMERIC:
            case Types.DECIMAL:
                if(decimalDigits > 0) {
                    javaType = "Double";
                } else {
                    if(columnSize < 10) {
                        javaType = "Integer";
                    } else {
                        javaType = "Long";
                    }
                }
                break;
            case Types.BLOB:
                javaType = "Byte[]";
                break;
            case Types.CLOB:
            case Types.CHAR:
            case Types.VARCHAR:
                javaType = "String";
                break;
            case Types.DATE:
            case Types.TIME:
            case Types.TIMESTAMP:
                javaType = "Date";
                break;
            default:
                break;
        }
        return javaType;
    }
	
    /**
     * 获取主键字段
     * @param meta
     * @return
     * @throws SQLException
     */
    protected List<String> getPrimaryKeys(DatabaseMetaData meta,String tableName) throws SQLException {
        ResultSet rs = meta.getPrimaryKeys(null, null, tableName.toUpperCase());
        List<String> primaryKeys = new ArrayList<String>();
        while(rs.next()) {
            primaryKeys.add(rs.getString("COLUMN_NAME"));
        }
        return primaryKeys;
    }
    
    protected ResultSet getColumns(DatabaseMetaData meta,String tableName) throws SQLException {
        // TODO Auto-generated method stub
        return meta.getColumns(null, null, tableName.toUpperCase(), null);
    }
}
